package com.bana.fvs.app.module.info.page;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bana.fvs.R;
import com.bana.fvs.app.base.BaseActivity;
import com.bana.fvs.app.module.info.entity.UpdatePwdBean;
import com.bana.fvs.app.mvp.contract.ChangePwdContract;
import com.bana.fvs.app.mvp.presenter.ChangePwdPresenter;
import com.wei.commonlibrary.net.CommException;
import com.wei.commonlibrary.utils.ToastUtil;

import butterknife.BindView;

/**
 * Author : yanftch
 * Date   : 2018/2/7
 * Time   : 17:48
 * Desc   : 修改密码
 */

public class ChangePwdActivity extends BaseActivity<ChangePwdPresenter> implements ChangePwdContract.IChangeView {
    private static final String TAG = "dah_ChangePwdActivity";
    @BindView(R.id.etOldPassword)
    EditText mOldPassword;
    @BindView(R.id.etNewPassword)
    EditText mNewPassword;
    @BindView(R.id.etConfirmPassword)
    EditText mConfirmPassword;
    @BindView(R.id.tvChangePwd)
    TextView mTvChangePwd;

    @Override
    public int setLayout() {
        return R.layout.activity_change_pwd;
    }

    @Override
    public void setTitle() {
        mBaseTitleBarView.setLeftDrawable(-1);
        mBaseTitleBarView.setTitleText(getString(R.string.tip_change_pwd));
        mBaseTitleBarView.setLeftTitleGone();
    }

    @Override
    public void initWidget() {
        mTvChangePwd.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        if (v.getId() == R.id.tvChangePwd) {
            if (checkInfo()) {
                http_changePwd();
            }
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter.setView(this);
    }

    @Override
    public void onTitleLeftPressed() {
        onBackPressed();
    }

    private void http_changePwd() {
        mPresenter.http_updatePwd(this, mOldPassword.getText().toString(), mNewPassword.getText().toString());
    }

    private boolean checkInfo() {
        String oldPassword = mOldPassword.getText().toString();
        String newPassword = mNewPassword.getText().toString();
        String newConfirmPassword = mConfirmPassword.getText().toString();
        if (TextUtils.isEmpty(oldPassword)) {
            ToastUtil.showShort(mContext, getString(R.string.tip_old_pwd_hint));
            return false;
        }
        if (TextUtils.isEmpty(newPassword)) {
            ToastUtil.showShort(mContext, getString(R.string.tip_new_pwd_hint));
            return false;
        }
        if (TextUtils.isEmpty(newConfirmPassword)) {
            ToastUtil.showShort(mContext, getString(R.string.tip_new_pwd_confirm_hint));
            return false;
        }
        if (!TextUtils.equals(newConfirmPassword, newPassword)) {
            ToastUtil.showShort(mContext, getString(R.string.tip_pwd_not_same));
            return false;
        }
        return true;
    }

    @Override
    public void onError(CommException e) {
        ToastUtil.showShort(mContext, e.getMessage());
    }

    @Override
    public void onResponse(UpdatePwdBean updatePwdBean) {
        if (updatePwdBean.getCzjg() == 0) {
            ToastUtil.showShort(mContext, getString(R.string.toast_change_pwd_success));
            finish();
        } else {
            ToastUtil.showShort(mContext, getString(R.string.toast_change_pwd_error));
        }
    }
}
